class_name StorageSystem
extends BaseSystem

## 游戏存储系统，用于持久化存储
## 一般是转成Json存到游戏目录下，也可能是转成Resource存储
#region 属性
const save_data_file_path = "res://save_data"
const save_data_file_name = "save_data.tres"
var save_data:SaveData
#endregion
#region 字段
#endregion
#region 信号
signal start_save
signal end_save
signal start_load
signal end_load
#endregion

#region 基类方法
func init_system():
	super.init_system()
	load_all()
#endregion
#region 公共方法
func get_save_data():
	return save_data
func save_to_res():
	if not save_data:
		save_data = SaveData.new()
	var err = ResourceSaver.save(save_data,save_data_file_path + "/" + save_data_file_name)
	if err == OK:
		print("SaveData保存成功")
	else:
		print("SaveData保存失败：" + err)
func load_from_res():
	var existed = FileAccess.file_exists(save_data_file_path + "/" + save_data_file_name)
	if not existed:
		print("SaveData不存在")
		save_data = SaveData.new()
		return
	save_data = ResourceLoader.load(save_data_file_path + "/" + save_data_file_name,"SaveData")
	if save_data:
		print("SaveData加载成功")
	else:
		print("SaveData加载失败")
		save_data = SaveData.new()
func save_all():
	start_save.emit()
	save_control()
	save_sound()
	#save_achievement()
	save_to_res()
	end_save.emit()
func load_all():
	start_load.emit()
	load_from_res()
	load_control()
	load_sound()
	load_achievement()
	end_load.emit()
# TODO 实现一些具体的信息存储方法，例如杀敌数
func save_control():
	if not save_data.control:
		save_data.control = {}
	for action in InputMap.get_actions():
		if action.begins_with("ui_"):
			continue
		save_data.control[action] = InputMap.action_get_events(action)[0]
func load_control():
	if not save_data.control or save_data.control.size() == 0:
		save_control()
	for action in save_data.control:
		InputMap.action_erase_event(action,InputMap.action_get_events(action)[0])
		InputMap.action_add_event(action,save_data.control[action])
func save_sound():
	if not save_data.sound:
		save_data.sound = {
			BGM = 50,
			SFX = 50,
		}
func load_sound():
	if not save_data.sound or save_data.sound.size() == 0:
		save_sound()
func save_achievement(achievement_dict:Dictionary):
	if not save_data.achievement:
		save_data.achievement = {}
	save_data.achievement = achievement_dict
func load_achievement():
	if not save_data.achievement or save_data.achievement.size() == 0:
		#save_achievement()
		pass
#endregion
#region 私有方法
#endregion
#region 生命周期函数
#endregion
